package com.zh.risk.controller;

import afu.org.checkerframework.checker.units.qual.A;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.risk.entity.RiskMerchTrans;
import com.zh.risk.entity.RiskModel;
import com.zh.risk.service.RiskMerchTransService;
import com.zh.risk.feign.work.MrcTransQL;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zh.common.entity.JSONResult;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 业务规则调用控制器
 * @Author: Liard
 * @Create in: 2020/7/10 10:48
 * @Version: 1.0.1
 */
@Api(value = "MrcTransQlController", tags = "业务规则调用控制器")
@RestController
@RequestMapping("/trans")
@Slf4j
public class MrcTransQlController {

    @Autowired
    private MrcTransQL transQL;

    @Autowired
    private RiskMerchTransService transService;


    @ApiOperation(value = "分页查询商户交易风控规则", tags = {"getTransRiskControl"}, notes = "分页查询商户交易风控规则")
    @GetMapping("/getTransRiskControl")
    public JSONResult<Object> getTransRiskControl(
            @ApiParam(value = "起始页") Integer pageNo,
            @ApiParam(value = "显示数量页面") Integer pageSize,
            @ApiParam(value = "商户交易风控实体信息", name = "riskMerchTrans") RiskMerchTrans riskMerchTrans) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            Page<RiskMerchTrans> page = new Page<>(pageNo, pageSize);
            IPage<RiskMerchTrans> result = transService.getRiskMerchTrans(page, riskMerchTrans);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加商户交易风控规则", tags = {"addTransRiskControl"}, notes = "添加商户交易风控规则")
    @PutMapping("/addTransRiskControl")
    @ResponseBody
    public JSONResult<Object> addTransRiskControl(@ApiParam(value = "商户交易风控实体信息", name = "riskMerchTrans") @RequestBody RiskMerchTrans riskMerchTrans) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (transService.addRiskMerchTrans(riskMerchTrans)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("添加完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改商户交易风控规则", tags = {"modTransRiskControl"}, notes = "修改商户交易风控规则")
    @PutMapping("/modTransRiskControl")
    @ResponseBody
    public JSONResult<Object> modTransRiskControl(@ApiParam(value = "商户交易风控实体信息", name = "riskMerchTrans") @RequestBody RiskMerchTrans riskMerchTrans) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (transService.modRiskMerchTrans(riskMerchTrans)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("添加完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除商户交易风控规则", tags = {"delTransRiskControl"}, notes = "删除商户交易风控规则")
    @DeleteMapping("/delTransRiskControl")
    public JSONResult<Object> delTransRiskControl(@ApiParam(value = "商户交易风控实体信息", name = "riskMerchTrans") RiskMerchTrans riskMerchTrans) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (transService.delRiskMerchTrans(riskMerchTrans)) {
                jsonResult.setMessage("操作完成!");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("删除失败!");
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "商户交易风控规则过滤", tags = {"mrcTransQl"}, notes = "商户交易风控规则过滤")
    @GetMapping("/mrc")
    public int mrcTransQl(@RequestParam(value = "pMrcId") @ApiParam(value = "平台商户编号", name = "pMrcId") String pMrcId) {
        try {
            transQL.MrcRiskControl(pMrcId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }


    @ApiOperation(value = "获取风控规则模型", tags = {"getRiskModel"}, notes = "获取风控规则模型")
    @GetMapping("/getRiskModel")
    public JSONResult<Object> getRiskModel(@RequestParam("id") String id, @RequestParam(value = "name", required = false) String name) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            List<RiskModel> models = transService.getRiskModel(id, name);
            jsonResult.setData(models);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }


    @ApiOperation(value = "删除风控规则模型", tags = {"delRiskModel"}, notes = "删除风控规则模型")
    @DeleteMapping("/delRiskModel")
    public JSONResult<Object> delRiskModel(@RequestParam("id") String id) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            boolean b = transService.delRiskModel(id);
            if (b) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改风控规则模型", tags = {"modRiskModel"}, notes = "修改风控规则模型")
    @PutMapping("/modRiskModel")
    @ResponseBody
    public JSONResult<Object> modRiskModel(@RequestBody RiskModel riskModel) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            RiskModel model = transService.modRiskModel(riskModel);
            if (Objects.nonNull(model)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("修改失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加指定规则风控模型", tags = {"addRiskModel"}, notes = "添加指定规则风控模型")
    @PostMapping("/model")
    @ResponseBody
    public JSONResult<Object> addRiskModel(@RequestBody RiskModel riskModel, @RequestParam("id") String id) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            RiskModel model = transService.addRiskModel(id, riskModel);
            if (Objects.nonNull(model)) {
                jsonResult.setMessage("操作完成!");
                jsonResult.setSuccess(true);
                jsonResult.setData(model);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("添加失败,model为null");
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

}
